package com.starsoft.frame.base.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.BaseRole;
import com.starsoft.frame.base.bean.BaseRolemenu;
import com.starsoft.frame.base.service.RoleService;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.StringUtil;


@RestController
@RequestMapping(value = "/base/set")
public class BaseRoleController {

	@Autowired
	RoleService roleService;
	
	@RequestMapping("/getrole")
	public List<BaseRole> getRole(){
		return roleService.getAllRole();
	}
	
	@RequestMapping("/saverole")
	public void saveRole(BaseRole role){
		roleService.saveRole(role);
	}
	
	@RequestMapping("/deleterole")
	public void deleteRole(BaseRole role){
		roleService.deleteRole(role);
	}
	
	@RequestMapping("/saverolemenu")
	public Map<String, Object> saveRoleMenu(BaseRole role, HttpServletRequest request){
		String menuIds = request.getParameter("menuIds");
		if(StringUtil.isEmpty(menuIds)){
			return WebUtil.errorModel("no menu");
		}
		menuIds= menuIds.substring(0, menuIds.length()-1);
		roleService.saveRoleMenu(role.getRoleId(), menuIds);
		return WebUtil.sucesseModel("保存成功");
	}

	@RequestMapping("/getrolemenu")
	public List<BaseRolemenu> getRoleMenu(BaseRole role){
		return roleService.getRoleMenu(role.getRoleId());
	}
	
}
